package com.industrial.im.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.industrial.common.core.utils.StringUtils;
import com.industrial.common.security.utils.SecurityUtils;
import com.industrial.im.domain.DispatchApplyExtension;
import com.industrial.im.domain.vo.ItemDetailVo;
import com.industrial.im.domain.vo.TaskVo;
import com.industrial.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.industrial.common.log.annotation.Log;
import com.industrial.common.log.enums.BusinessType;
import com.industrial.common.security.annotation.RequiresPermissions;
import com.industrial.im.domain.DispatchTaskExecutionManage;
import com.industrial.im.service.IDispatchTaskExecutionManageService;
import com.industrial.common.core.web.controller.BaseController;
import com.industrial.common.core.web.domain.AjaxResult;
import com.industrial.common.core.utils.poi.ExcelUtil;
import com.industrial.common.core.web.page.TableDataInfo;

/**
 * 任务执行管理Controller
 * 
 * @author SangGuangCan
 * @date 2024-03-08
 */
@RestController
@RequestMapping("/task")
public class DispatchTaskExecutionManageController extends BaseController
{
    @Autowired
    private IDispatchTaskExecutionManageService dispatchTaskExecutionManageService;

    /**
     * 查询任务执行管理列表
     */
    @RequiresPermissions("task:task:list")
    @GetMapping("/list")
    public TableDataInfo list(DispatchTaskExecutionManage dispatchTaskExecutionManage)
    {
        startPage();
        List<DispatchTaskExecutionManage> list = dispatchTaskExecutionManageService.selectDispatchTaskExecutionManageList(dispatchTaskExecutionManage);
        return getDataTable(list);
    }

    /**
     * 导出任务执行管理列表
     */
    @RequiresPermissions("task:task:export")
    @Log(title = "任务执行管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DispatchTaskExecutionManage dispatchTaskExecutionManage)
    {
        List<DispatchTaskExecutionManage> list = dispatchTaskExecutionManageService.selectDispatchTaskExecutionManageList(dispatchTaskExecutionManage);
        ExcelUtil<DispatchTaskExecutionManage> util = new ExcelUtil<DispatchTaskExecutionManage>(DispatchTaskExecutionManage.class);
        util.exportExcel(response, list, "任务执行管理数据");
    }

    /**
     * 获取任务执行管理详细信息
     */
    @RequiresPermissions("task:task:query")
    @GetMapping(value = "/{baseId}")
    public AjaxResult getInfo(@PathVariable("baseId") String baseId)
    {
        return success(dispatchTaskExecutionManageService.selectDispatchTaskExecutionManageByBaseId(baseId));
    }

    /**
     * 新增任务执行管理
     */
    @RequiresPermissions("task:task:add")
    @Log(title = "任务执行管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DispatchTaskExecutionManage dispatchTaskExecutionManage)
    {
        return toAjax(dispatchTaskExecutionManageService.insertDispatchTaskExecutionManage(dispatchTaskExecutionManage));
    }

    /**
     * 修改任务执行管理
     */
    @RequiresPermissions("task:task:edit")
    @Log(title = "任务执行管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DispatchTaskExecutionManage dispatchTaskExecutionManage)
    {
        return toAjax(dispatchTaskExecutionManageService.updateDispatchTaskExecutionManage(dispatchTaskExecutionManage));
    }

    /**
     * 删除任务执行管理
     */
    @RequiresPermissions("task:task:remove")
    @Log(title = "任务执行管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{baseIds}")
    public AjaxResult remove(@PathVariable String[] baseIds)
    {
        return toAjax(dispatchTaskExecutionManageService.deleteDispatchTaskExecutionManageByBaseIds(baseIds));
    }

    /**
     * 查询列表
     * @return
     */
    @GetMapping("/queryList")
    public TableDataInfo queryList(TaskVo taskVo){

        startPage();
        List<TaskVo> result = dispatchTaskExecutionManageService.queryList(taskVo);
        return getDataTable(result);
    }

    /**
     * <p>
     *     查询签收的负责人或者是参与人
     * </p>
     * @param itemId 事项的ID
     * @return
     */
    @GetMapping("/queryMainHeader/{itemId}")
    public AjaxResult queryMainHeader(@PathVariable("") String itemId){
        List<SysUser> result = dispatchTaskExecutionManageService.queryMainHeader(itemId);
        return AjaxResult.success(result);
    }

    /**
     * <p>
     *     保存事项负责人和事项参与人
     * </p>
     * @param params
     * @return
     */
    @PostMapping("/saveMainHeaderAndCyrInfo")
    public AjaxResult saveMainHeaderAndCyrInfo(@RequestBody DispatchTaskExecutionManage params){
        dispatchTaskExecutionManageService.saveMainHeaderAndCyrInfo(params);
        return AjaxResult.success();
    }

    /**
     * 查询事项页面详情
     * @param baseId
     * @return
     */
    @GetMapping("/queryItemDetailInfo/{baseId}")
    public AjaxResult queryItemDetailInfo(@PathVariable("baseId") String baseId){
        if(StringUtils.isBlank(baseId) || StringUtils.equals(baseId,"null")){
            return AjaxResult.success("该任务还没有签收,请签收后查看数据");
        }
        ItemDetailVo itemDetailVo = dispatchTaskExecutionManageService.queryItemDetailInfo(baseId);
        return AjaxResult.success(itemDetailVo);
    }
}
